﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ecommerce.Model;
using Ecommerce.Web.Code;

namespace Ecommerce.Web.Cliente
{
    public partial class Pedidos : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                CarregarCombo();
                CarregarPedidos();

                if (ViewState["PagedDataSource"] != null)
                    CreatePaging((int)ViewState["PagedDataSource"]);
            }
            else
            {
                if (ViewState["PagedDataSource"] != null)
                    CreatePaging((int)ViewState["PagedDataSource"]);
            }
        } 

        public int CurrentPage
        {
            get
            {
                object o = this.ViewState["_CurrentPage"];
                if (o == null)
                    return 0;
                else
                    return (int)o;
            }

            set
            {
                this.ViewState["_CurrentPage"] = value;
            }
        }

        private void CarregarCombo()
        {
            ListItemCollection lst = new ListItemCollection();
            lst.Add(new ListItem("5","5"));
            lst.Add(new ListItem("10", "10"));
            lst.Add(new ListItem("15", "15"));
            ddlNumItens.DataSource = lst;
            ddlNumItens.DataBind();
        }

        private void CarregarPedidos()
        {
            var pedidos = PedidoModel.Instance.ListarPedidosUsuario(base.IdentificadorUsuarioLogado);
            
            PagedDataSource objPds = new PagedDataSource();
            objPds.DataSource = pedidos;
            objPds.AllowPaging = true;
            objPds.PageSize = Convert.ToInt32(ddlNumItens.SelectedValue);

            objPds.CurrentPageIndex = CurrentPage;

            lblCurrentPage.Text = "Resultado(s): " + (CurrentPage + 1).ToString() + " de " + objPds.PageCount.ToString();

            lkbPrev.Enabled = !objPds.IsFirstPage;
            lkbNext.Enabled = !objPds.IsLastPage;

            ViewState["PagedDataSource"] = objPds.PageCount;

            rptPedido.DataSource = objPds;
            rptPedido.DataBind();
        }

        protected void rptPedido_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var item = (tb_ped_pedido) e.Item.DataItem;

                Label lblNumPedido = (Label)e.Item.FindControl("lblNumPedido");
                Label lblStatus = (Label)e.Item.FindControl("lblStatus");
                Label lblDtPedido = (Label)e.Item.FindControl("lblDtPedido");
                Label lblVlrTotal = (Label)e.Item.FindControl("lblVlrTotal");
                LinkButton lkbDetalhes = (LinkButton)e.Item.FindControl("lkbDetalhes");

                lblNumPedido.Text = item.ped_idt_pedido.ToString();
                lblStatus.Text = item.tb_spl_status_pedido_log.First().tb_spe_status_pedido.spe_des_status_pedido;
                lblDtPedido.Text = item.ped_dat_criacao.ToString();
                lblVlrTotal.Text = item.ped_vlr_total_pedido.Value.ToString("c");
                lkbDetalhes.CommandArgument = item.ped_idt_pedido.ToString();
            }
        }

        protected void rptPedido_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string idtPedido = e.CommandArgument.ToString();
            Response.Redirect("DetalhePedido.aspx?idtPedido=" + idtPedido);
        }
 
        protected void CreatePaging(int pds)
        {
            for (int i = 0; i < pds; i++)
            {
                LinkButton link = new LinkButton();
                link.Text = (i + 1).ToString();
                link.ID = string.Concat("txt_", i.ToString());
                link.Click += new EventHandler(link_Click);
                link.CssClass = "lkb_paginacao";
                dvPaginacao.Controls.Add(link);

                LiteralControl space = new LiteralControl("&nbsp;");
                dvPaginacao.Controls.Add(space);
            }
        }

        protected void link_Click(object sender, EventArgs e)
        {
            LinkButton link = (LinkButton)sender;
            CurrentPage = Convert.ToInt32(link.Text) - 1;
            CarregarPedidos();
        }


        protected void lkbPrev_Click(object sender, EventArgs e)
        {
            CurrentPage -= 1;
            CarregarPedidos();

        }

        protected void lkbNext_Click(object sender, EventArgs e)
        {
            CurrentPage += 1;
            CarregarPedidos();
        }

        protected void ddlNumItens_SelectedIndexChanged(object sender, EventArgs e)
        {
            CarregarPedidos();
        }

    }
}